package com.stottlerhenke.simbionic.editor;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;

import javax.swing.Icon;

public class SB_Parameter extends SB_Variable 
{
    private static final long serialVersionUID = 2302585093L + 6;

    public static final int kIn = 0;
    public static final int kOut = 1;
    public static final int kInOut = 2;
    public static final String[] kDirectionNames = { "in", "out", "in/out" };

    private int _direction;	// binding direction


    public SB_Parameter()  // constructor for Externalizable object
    {
    }

    public SB_Parameter(String name)
    {
        super(name);
        _direction = kIn;
    }

    public SB_Parameter(String name, int type, int direction)
    {
        super(name, type);
        _direction = direction;
    }

    public String toString()
    {
      if (_editing)
        return _name;
      else
      {
        if (_fullName == null)
            _fullName = _name + " : " + getTypeName() + " [" + kDirectionNames[_direction] + "]";
        return _fullName;
      }
    }

    public int getDirection() { return _direction; }
    public void setDirection(int direction) { _direction = direction; _fullName = null; }
    public static int directionToInt(String direction){
    	for (int i=0;i<kDirectionNames.length;i++){
    		if (kDirectionNames[i].equals(direction))
    			return i;
    	}
    	return -1;
    }


    public Icon getIcon()
    {
        if (_icon == null)
            _icon = Util.getImageIcon("Parameter.gif");
        return _icon;
    }

    public void compileSIM(BufferedWriter out, SB_TypeManager typeManager)
    {
      try
      {
        super.compileSIM(out, typeManager);
        out.write(" " + _direction);
      }
      catch (IOException exception)
      {
        System.err.println("i/o exception");
      }
    }

    public void writeExternal(ObjectOutput out) throws IOException
    {
      super.writeExternal(out);
      out.writeInt(_direction);
    }

    public void readExternal(ObjectInput in) throws ClassNotFoundException, IOException
    {
      super.readExternal(in);
      _direction = in.readInt();
    }
    
    public Object clone()
    {
        return new SB_Parameter(_name,_type,_direction);
    }

}
